import {
	defineStore
} from 'pinia'
import {
	getconfig
} from '@/api/index.js'


let esource = null

// 主体store
export const useMainStore = defineStore('main', {
	state: () => ({
		//app版本  这边一般放配置信息或者登录用户信息
		version: "1.0.0",
		jumpLogined: false,

		stockData: null, //股票数据
		Symbol: null, //股票代码
		sysConfig: uni.getStorageSync('sysConfig') || {}
	}),

	// action
	actions: {
		SetJumpLogined(bool) {
			this.jumpLogined = bool
		},

		diffUpStockData(data) {
			if (!this.stockData) {
				this.stockData = data
			} else {
				for (let key in data) {
					this.stockData[key] = data[key]
				}
			}
		},

		setSysConfig(config) {
			this.sysConfig = config;
			uni.setStorageSync('sysConfig', config)
		},
		setSymbol(Symbol) {
			this.Symbol = Symbol
		},
		destoryEventSource() {
			// if (esource) { //如果存在 关闭上一次连接，重新建立连接
			// 	esource.close()
			// 	esource = null
			// }
			this.stockData = null;
			this.Symbol = null
		},

		getConfig(opts) {
			return new Promise((resolve, reject) => {
				getconfig().then(res => {
					// commit('setSysConfig', res.data)
					this.setSysConfig(res.data)
					// #ifdef H5
					document.title = res.data.web_name;
					// #endif
					resolve()
				})
			}).catch((e) => {})
		},

		creartEventSource(opts) {
			return new Promise((resolve, reject) => {
				if (this.Symbol != opts.symbol) { //不同一个股票代码
					if (esource) {
						// commit('destoryEventSource')
						this.destoryEventSource()
					}
					// 将股票代码转为东方财富的码
					let aryData = opts.symbol.split('.')
					var market = 0; //市场代码
					if (aryData[1].toLowerCase() == 'sh') {
						market = 1
					} else if (aryData[1].toLowerCase() == 'sz' || aryData[1].toLowerCase() == 'bj') {
						market = 0
					}
					let internalSymbol = aryData[0]
					// 将股票代码转
					let url = `https://${random(20,90)}.push2.eastmoney.com/api/qt/stock/sse` //随机行情服务器，
					let query =
						`?fields=f58,f57,f43,f59,f169,f170,f152,f177,f111,f46,f60,f44,f45,f47,f137,f138,f139,f140,f141,f142,f143,f144,f145,f146,f147,f148,f149,f260,f48,f261,f279,f277,f278,f288,f19,f17,f531,f15,f13,f11,f20,f18,f16,f14,f12,f39,f37,f35,f33,f31,f40,f38,f36,f34,f32,f211,f212,f213,f214,f215,f210,f209,f208,f207,f206,f161,f49,f171,f50,f86,f84,f85,f168,f108,f116,f167,f164,f162,f163,f92,f71,f117,f51,f52,f191,f192,f262&mpi=1000&invt=2&fltt=1&secid=${market}.${internalSymbol}&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=|0|0|0|web`

					esource = new EventSource(url + query);
				} else if (state.Symbol == opts.symbol && esource) {
					resolve()
					opts.onMsg(state.stockData)
				}
				// commit('setSymbol', opts.symbol)

				this.setSymbol(opts.symbol)
				esource.onmessage = (event) => {
					let res = JSON.parse(event.data);
					if (opts.onMsg && typeof opts.onMsg == 'function') {
						// commit('diffUpStockData', res.data)
						this.diffUpStockData(res.data)
						opts.onMsg(res.data)
					}
				}
				esource.onopen = function(event) {
					console.log("open:" + JSON.stringify(event));
					resolve()
				};
				esource.onerror = function(event) {
					console.log("error:" + JSON.stringify(event) + JSON.stringify(es));
					reject()
				}
			})
		}

	}
})